//Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Diagnostics;

namespace Microsoft.Office.OneNote
{
	public class Notebook : Folder
	{
		public Notebook()
		{
		}

		public Notebook(string path)
		{
			this.Path = path;
		}

		/// <summary>
		/// Clones the Folder.
		/// </summary>
		/// <returns>A deep copy of this Folder.</returns>
		public override object Clone()
		{
			Notebook clone = new Notebook();
			clone.Id = this.Id;

			return clone;
		}

		/// <summary>
		/// Useful for debugging.
		/// </summary>
		/// <returns>A string representation of the Notebook.</returns>
		public override string ToString()
		{
			return Nickname;
		}

		/// <summary>
		/// Deserializes the <see cref="Notebook"/> object from OneNote's xml 
		/// representation.
		/// </summary>
		/// <param name="node">
		/// A reference to the XML node that we should deserialize ourselves
		/// from.
		/// </param>
		protected internal override void DeserializeFromXml(XmlNode node)
		{
			XmlElement element = (XmlElement) node;

			this.Nickname = element.GetAttribute("nickname");
			if (element.HasAttribute("color") && element.GetAttribute("color") != "none")
				this.Color = ColorTranslator.FromHtml(element.GetAttribute("color"));

			base.DeserializeFromXml(node);
		}

		/// <summary>
		/// Used while committing all pending additions to commit any newly created
		/// node to OneNote.
		/// </summary>
		/// <param name="serializedId">The new ID of this node.</param>
		protected internal override void AddHierarchyToOneNote(out string serializedId)
		{
			Debug.Assert(this.Id == ObjectId.Empty);

			// Create the notebook via OpenHierarchy:
			string parentId = null;
			if (this.Parent is HierarchyNode)
			{
				parentId = ((HierarchyNode) this.Parent).Id.ToString();
			}

			Application.Server.OpenHierarchy(this.Path, parentId, out serializedId, CreateFileType.Notebook);
		}

		/// <summary>
		/// Helper method used while serializing to allow every folder sub-class to serialize
		/// it's properties...
		/// </summary>
		/// <param name="serializedNode">Our serialized XmlElement</param>
		protected internal override void SerializeFolderProperties(XmlElement serializedElement)
		{
			serializedElement.SetAttribute("nickname", this.Nickname);

			string serializedColor = "none";
			if (this.Color != Color.Empty)
				serializedColor = ColorTranslator.ToHtml(Color.FromArgb(this.Color.ToArgb()));
			serializedElement.SetAttribute("color", serializedColor);

			base.SerializeFolderProperties(serializedElement);
		}

		public string Nickname
		{
			get { return GetProperty_Else("nickname", ""); }
			set { SetProperty("nickname", value); }
		}

		public Color Color
		{
			get { return GetProperty_Else("color", Color.Empty); }
			set { SetProperty("color", value); }
		}

		/// <summary>
		/// The serialized Xml element type 
		/// </summary>
		protected override internal string SerializedXmlType
		{
			get { return "Notebook"; }
		}
	}
}
